// 妥协: NodeJS.Timeout不能使用
const timeOutStart: Ref<NodeJS.Timeout | undefined> = shallowRef();
// 防御性编程，防止快速的第二次加载 产生意外
const timeOutFinish: Ref<NodeJS.Timeout | undefined> = shallowRef();
export const useMyLoadingStore = defineStore({
  id: "myLoadingStore",
  state: () => ({
    loadingValue: undefined as number | undefined,
  }),
  actions: {
    start() {
      if (timeOutFinish.value)
        clearTimeout(timeOutFinish.value);

      this.loadingValue = 0;
      timeOutStart.value = setTimeout(() => {
        this.loadingValue = 30;
      }, 300);
    },
    finish() {
      if (timeOutStart.value)
        clearTimeout(timeOutStart.value);

      this.loadingValue = 100;
      timeOutFinish.value = setTimeout(() => {
        this.loadingValue = undefined;
      }, 300);
    },
  },
  getters: {
    isLoading: (state) => {
      return state.loadingValue !== undefined;
    },
  },
});
